home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 01 / recurse.c < prev    next >
C/C++ Source or Header  |  1990-12-04  |  907b  |  45 lines

  1. /*  Consider in C:  */
  2.  
  3. struct tree {
  4.    char info;
  5.    struct tree *left;
  6.    struct tree *right;
  7.    } node;
  8.  
  9. typedef struct tree TREE;
  10.  
  11. TREE *stree(TREE *root, TREE *cur, char info){
  12.    if (!cur){
  13.       if (!(cur = malloc(sizeof(node))) {
  14.          printf("Out of memory\n");
  15.          exit(0);
  16.          }
  17.       cur->left = cur->right = 0;
  18.       if (info<root->info)
  19.          root->left = cur;
  20.       else
  21.          root->right = cur;
  22.       return cur;
  23.       }
  24.    if (info<cur->info)
  25.       stree(cur,cur->left,info);  /* to left */
  26.    else if (info>cur->info)
  27.       stree(cur,cur->right,info); /* to right */
  28.    }
  29.  
  30.  
  31. @CODE = void dfo( TREE *root ) {
  32.    if (!root)
  33.       return;
  34.    dfo(root->left);
  35.    printf("info --> %c ",root->info);
  36.    dfo(root->right);
  37.    }
  38.  
  39. void initialize_tree(void){
  40.    while (!done)
  41.       stree(root,get_next_node(),info)
  42.    dfo(root);
  43.    } 
  44.  
  45.